home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 25 / AACD 25.iso / AACD / Magazine / Online / QMail / utils / minichmod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-30  |  2.6 KB  |  107 lines

  1. /* Amigatized mini clone of chmod, capable of setting Amiga specific
  2.  * file/drawer attributes.
  3.  */
  4.  
  5. #include <dos/dos.h>
  6. #include <errno.h>
  7. #include <stdlib.h>
  8. #include <stdio.h> /* Needed by ix(_amiga).h. */
  9. /* Hack to find out if include files are for ixemul version 47+ or not. */
  10. #define __pos__
  11. #ifndef CTOBPTR
  12. #define ix_select extselect
  13. #include <ix_amiga.h>
  14. #else
  15. #define __INCLV47__
  16. #include <ix.h>
  17. #endif
  18. #undef __pos__
  19.  
  20. #ifndef __INCLV47__
  21. #define ix_chmod(a,b) achmod(a,b)
  22. #endif
  23.  
  24. void usage (void);
  25.  
  26. int main (int argc, char *argv[])
  27. {
  28.   unsigned long attrs, protbits;
  29.   char *end;
  30.   unsigned long i;
  31.  
  32.   /* Needs an attribute mask and at least one file/drawer name. */
  33.   if (argc <= 2)
  34.   {
  35.     usage ();
  36.     return (5);
  37.   }
  38.  
  39.   /* Attribute string must not be empty. */
  40.   if (argv[1][0] == '\0')
  41.   {
  42.     usage ();
  43.     return (5);
  44.   }
  45.  
  46.   errno = 0;
  47.   attrs = strtol (argv[1], &end, 16);
  48.  
  49.   /* Attribute string must be fully convertible and not overflow. */
  50.   if (*end != '\0' || errno != 0)
  51.   {
  52.     usage ();
  53.     return (5);
  54.   }
  55.  
  56.   /* Convert the attributes to protection bits required by ix_chmod(). */
  57.   /* User delete/execute/write/read bits. */
  58.   protbits = (attrs & 0x00F00) >> 2*4;
  59.  
  60.   /* User delete/execute/write/read bits are "active low". */
  61.   protbits ^= FIBF_DELETE | FIBF_EXECUTE | FIBF_WRITE | FIBF_READ;
  62.  
  63.   /* Group delete/execute/write/read bits. */
  64.   protbits |= (attrs & 0x000F0) << 1*4;
  65.  
  66.   /* Other delete/execute/write/read bits. */
  67.   protbits |= (attrs & 0x0000F) << 3*4;
  68.  
  69.   /* Lower attribute bits (archive/pure/script). */
  70.   protbits |= (attrs & 0x0F000) >> 2*4;
  71.  
  72.   /* Upper attribute bits (setgid/setuid). */
  73.   protbits |= (attrs & 0xF0000) << 12;
  74.  
  75.   for (i = 2; i < argc; i ++)
  76.   {
  77.     ix_chmod (argv[i], protbits);
  78.   }
  79.  
  80.   return (0);
  81. }
  82.  
  83. void usage (void)
  84. {
  85.   puts ("Usage: minichmod attributes file ...\n"
  86.         "\n"
  87.         "Attributes are specified as a five digit hexadecimal number\n"
  88.         "(the letters A-F are the digits 10-15).\n"
  89.         "The five digits 'mpugo' work like this:\n"
  90.         "m: MultiUser attributes.\n"
  91.         "   8 = Change owner during execution.\n"
  92.         "   4 = Change group during execution.\n"
  93.         "\n"
  94.         "p: Standard file attributes.\n"
  95.         "   4 = File is a script file.\n"
  96.         "   2 = File is a residentable programme.\n"
  97.         "   1 = File has been archived (cleared on changes).\n"
  98.         "\n"
  99.         "ugo: User, group, other permissions.\n"
  100.         "   8 = File is readable.\n"
  101.         "   4 = File is writable.\n"
  102.         "   2 = File is executable.\n"
  103.         "   1 = File is deletable.\n"
  104.         "\n"
  105.         "Attributes are combined by adding them.");
  106. }
  107.